#define OPENCV_TRAITS_ENABLE_DEPRECATED
#include "MainWindow.h"
#include "pch.h"

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    MainWindow window;

    auto model = fdeep::load_model("mnist.json");

    QObject::connect(
        window.btn, &QPushButton::clicked, &app, [&window, &model] {
            using std::vector;
            auto qimg = window.canvas->getImage().convertToFormat(
                QImage::Format_Grayscale8);
            Mat mat(qimg.height(), qimg.width(), CV_8UC1, qimg.bits(),
                    qimg.bytesPerLine());
            vector<vector<Point>> contours;
            findContours(mat, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
            QString str;

            for (auto &contour : contours) {
                auto rect = boundingRect(contour);
                str += QString::number(
                    predictImage(expandImage(mat(rect)), model));
                str += ' ';
            }
            window.label->setText(str.chopped(1));
        });
    QObject::connect(window.clearBtn, &QPushButton::clicked, &app, [&window] {
        window.canvas->clear();
        window.label->setText("None");
    });
    QObject::connect(window.slider, &QSlider::valueChanged, &app, [&window] {
        window.canvas->setWidth(window.slider->value());
    });

    window.show();
    return app.exec();
}